import customtkinter as ctk
from .ui.views.home_view import HomeView
# 1. 导入我们新的视图
from .ui.views.bank_reconciliation_view import BankReconciliationView


class MainWindow(ctk.CTk):
    """
    应用程序的主窗口。
    """

    def __init__(self):
        super().__init__()

        # --- 窗口基本设置 (这部分代码保持不变) ---
        self.title("财务工具箱")
        self.geometry("900x600")
        ctk.set_appearance_mode("System")
        ctk.set_default_color_theme("blue")

        # --- 配置主窗口网格布局 (这部分代码保持不变) ---
        self.grid_columnconfigure(1, weight=1)
        self.grid_rowconfigure(0, weight=1)

        # --- 创建左侧导航栏 ---
        self.nav_frame = ctk.CTkFrame(self, width=150, corner_radius=0)
        self.nav_frame.grid(row=0, column=0, sticky="nsew")
        self.nav_frame.grid_rowconfigure(5, weight=1)

        self.nav_label = ctk.CTkLabel(self.nav_frame, text="功能列表",
                                      font=ctk.CTkFont(size=18, weight="bold"))
        self.nav_label.grid(row=0, column=0, padx=20, pady=(20, 10))

        # 导航按钮
        self.home_button = ctk.CTkButton(self.nav_frame, text="主页", command=self.show_home_view)
        self.home_button.grid(row=1, column=0, padx=20, pady=10, sticky="ew")

        # 2. 修改按钮文本和命令
        self.reconciliation_button = ctk.CTkButton(self.nav_frame, text="余额调节表",
                                                   command=self.show_reconciliation_view)
        self.reconciliation_button.grid(row=2, column=0, padx=20, pady=10, sticky="ew")

        # --- 创建右侧内容显示区 (这部分代码保持不变) ---
        self.content_frame = ctk.CTkFrame(self)
        self.content_frame.grid(row=0, column=1, sticky="nsew", padx=20, pady=20)
        self.content_frame.grid_columnconfigure(0, weight=1)
        self.content_frame.grid_rowconfigure(0, weight=1)

        # --- 初始化所有视图 ---
        # 3. 将新视图添加到views字典中
        self.views = {
            "home": HomeView(self.content_frame),
            "reconciliation": BankReconciliationView(self.content_frame)
        }

        # --- 默认显示主页视图 ---
        self.show_home_view()

    def show_view(self, view_name):
        """通用视图切换函数"""
        for view in self.views.values():
            view.grid_forget()

        view_to_show = self.views.get(view_name)
        if view_to_show:
            view_to_show.grid(row=0, column=0, sticky="nsew")

    def show_home_view(self):
        self.show_view("home")

    # 4. 创建新的函数来显示我们的功能页面
    def show_reconciliation_view(self):
        self.show_view("reconciliation")